package com.leetcode.partition3;

/**
 * @author `RKC`
 * @date 2021/9/30 8:10
 */
public class LC223矩形面积 {

    public static int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        return math(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2);
    }

    private static int math(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        int x1 = Math.max(ax1, bx1), y1 = Math.max(ay1, by1);
        int x2 = Math.min(ax2, bx2), y2 = Math.min(ay2, by2);
        int ans = (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1);
        if (x1 >= x2 || y1 >= y2) return ans;
        return ans - (x2 - x1) * (y2 - y1);
    }
}
